package org.csu.pet_store_2.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.pet_store_2.Common.CommonResponse;
import org.csu.pet_store_2.Domain.Cart;
import org.csu.pet_store_2.Service.CartService;
import org.csu.pet_store_2.persistence.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@Transactional
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private CartMapper cartMapper;


    /*
    添加item到购物车；
    post方法
    发送数据的格式为Body下的row json
     {
     "userId":"1234",
     "itemId":"EST-19",
     "quantity":4
    }
    返回结果：
   {
    "status": 0,
    "msg": "SUCCESS",
    "data": "添加成功"
    }
     */
    @PostMapping("/addtocart")
    public CommonResponse<String> addtocart(@RequestBody Cart cart){
        cartService.additemtocart(cart.getUserId(),cart.getItemId(),cart.getQuantity());
        return CommonResponse.creatForSuccess("添加成功");
    }
    /*
    从购物车删除：
        post方法
    发送数据的格式为Body下的row json
     {
     "userId":"1234",
     "itemId":"EST-19",
     "quantity":4
    }
    返回结果：
    {
    "status": 0,
    "msg": "SUCCESS",
    "data": "删除成功"
    }
     */
    @PostMapping("/deletefromcart")
    public CommonResponse<String> deletfromcart(@RequestBody Cart cart){
        cartService.deletefromcart(cart.getUserId(),cart.getItemId());
        return CommonResponse.creatForSuccess("删除成功");
    }
    /*
    根据userId获取购物车的内容
     http://localhost:8080/carts?userId=1234
     {
    "status": 0,
    "msg": "SUCCESS",
    "data": [
        {
            "userId": "1234",
            "itemId": "EST-16",
            "quantity": 2
        },
        {
            "userId": "1234",
            "itemId": "EST-18",
            "quantity": 1
        },
        {
            "userId": "1234",
            "itemId": "EST-19",
            "quantity": 4
        }
    ]
    }
     */
    @GetMapping("/carts")
    public CommonResponse<ArrayList<Cart>> getCartByUserId(@RequestParam("userId")String userID) {
    ArrayList<Cart> cartArrayList = cartService.getCartByUserId(userID);
    return CommonResponse.creatForSuccess(cartArrayList);
    }
    /*
    修改quantity的方法
    post方法
    发送数据的格式为Body下的row json
     {
     "userId":"1234",
     "itemId":"EST-19",
     "quantity":15
    }
    返回数据：
    {
    "status": 0,
    "msg": "SUCCESS",
    "data": "更新成功"
    }
     */
    @PostMapping("/updatequantity")
    public CommonResponse<String> updatequantity(@RequestBody Cart cart){
        cartService.updatequantity(cart.getUserId(),cart.getItemId(),cart.getQuantity());
        return CommonResponse.creatForSuccess("更新成功");
    }
    /*
    清空购物车
    这个用于点击按钮清空购物车
    只需传入userId
     */
    @GetMapping("/cleancart")
    public CommonResponse<String> cleancart(@RequestParam("userid") String userid)
    {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("userid",userid);
        cartMapper.delete(queryWrapper);
        return CommonResponse.creatForSuccess("清空成功");
    }

}